package com.test.learnaidl.bean;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import androidx.annotation.Nullable;

/**
 * Java 如何重写对象的 equals 方法和 hashCode 方法  https://www.cnblogs.com/yuxiaole/p/9570850.html
 *
 *              java比较两个对象是否相等
 *             https://www.cnblogs.com/expiator/p/7813247.html
 *
 *             java比较两个对象,可以通过重写equals()实现。
 *
 *             1.比较对象时， ==比较的是地址，而equals()比较的是对象内容
 *
 *             2.重写equals()方法必须重写hashCode()
 *
 *
 *             UUID.randomUUID()生成唯一识别码 https://www.cnblogs.com/xiaohuizhenyoucai/p/11647547.html
 *
 *              注意 这里 PersonBean不能和 PersonBean.aidl 在同一个目录里，但是它们的包名要一样
 *
 *
 */
public class PersonBean implements Parcelable {
    private String name;
    private int age;
    private String id;//UUID.randomUUID().toString();

    public PersonBean() {// 无参构造方法必须写上
    }

    public PersonBean(String name, int age, String id) {
        this.name = name;
        this.age = age;
        this.id = id;
    }

    protected PersonBean(Parcel in) {
        name = in.readString();
        age = in.readInt();
        id = in.readString();
    }

    public PersonBean readFromParcel(Parcel in) { //注意这里需要手动添加 readFromParcel 方法
        name = in.readString();
        age = in.readInt();
        id = in.readString();
        return this;
    }

    public static final Creator<PersonBean> CREATOR = new Creator<PersonBean>() {
        @Override
        public PersonBean createFromParcel(Parcel in) {
            return new PersonBean(in);
        }

        @Override
        public PersonBean[] newArray(int size) {
            return new PersonBean[size];
        }

    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(age);
        dest.writeString(id);
    }

    @Override
    public int hashCode() {
        int resultHashCode = super.hashCode();
//        Log.i("==", "重新hashCode super.hashCode() = " + super.hashCode());
//        int resultHashCode = 0;
        if (!TextUtils.isEmpty(name)) {
            resultHashCode += name.hashCode();
        }

        if (age != 0) {
            resultHashCode += age;
        }

        if (!TextUtils.isEmpty(id)) {
            resultHashCode += id.hashCode();
        }

        return resultHashCode;
    }

    //Java 如何重写对象的 equals 方法和 hashCode 方法  https://www.cnblogs.com/yuxiaole/p/9570850.html

    @Override
    public boolean equals(@Nullable Object obj) {
        if (obj == null) {
            return false;
        }

        if (this == obj) {
            return true;//地址相等
        }

        if (obj instanceof PersonBean) {
            PersonBean personBean = (PersonBean) obj;
            if (this.id == personBean.id && TextUtils.equals(personBean.name, this.name)) {
                return true;
            }
        }
        return false;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getId() {
        return id;
    }

    @Override
    public String toString() {
        return "PersonBean{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", id=" + id +
                '}';
    }
}
